// pages/goods_detail/index.js
import { request } from "../../request/request.js";
import {showToast} from "../../utils/asyncWx.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id:2,
    /*goodDetail:{ 
      "goods_id": 8888,
      "cat_id": 1085,
      "goods_name": "spike 经典武士大马士革直刀(微型) 户外野营直刀 收藏礼品刀 饰品刀具",
      "goods_price": 500,
      "goods_number": 100,
      "goods_weight": 100,
      "goods_introduce": "富文本内容",
      "goods_state": 2,
      "is_del": "0",
      "add_time": 1516361489,
      "upd_time": 1516361489,
      "delete_time": null,
      "hot_mumber": 0,
      "is_promote": false,
      "cat_one_id": 995,
      "cat_two_id": 1075,
      "cat_three_id": 1085,
      "goods_cat": "995,1075,1085",
      "pics": [
          {
              "pics_id": 38711,
              "goods_id": 8888,
              "pics_big": "http://image1.suning.cn/uimg/b2c/newcatentries/0070134290-000000000149003877_1_800x800.jpg",
              "pics_mid": "http://image1.suning.cn/uimg/b2c/newcatentries/0070134290-000000000149003877_1_400x400.jpg",
              "pics_sma": "http://image1.suning.cn/uimg/b2c/newcatentries/0070134290-000000000149003877_1_200x200.jpg",
              "pics_big_url": "http://image1.suning.cn/uimg/b2c/newcatentries/0070134290-000000000149003877_1_800x800.jpg",
              "pics_mid_url": "http://image1.suning.cn/uimg/b2c/newcatentries/0070134290-000000000149003877_1_400x400.jpg",
              "pics_sma_url": "http://image1.suning.cn/uimg/b2c/newcatentries/0070134290-000000000149003877_1_200x200.jpg"
          }
      ]
     
    },*/
    goodDetail:{},
    isCollect:false


  },
  GoodsInfo:{},
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // console.log(this.data.goodDetail);
    
    /*console.log(options);
    this.data.goods_id=options.goods_id;*/
    /*this.GoodsInfo.goods_name=this.data.goodDetail.goods_name;
    this.GoodsInfo.goods_price=this.data.goodDetail.goods_price;
    this.GoodsInfo.goods_introduce=this.data.goodDetail.goods_introduce;
    this.GoodsInfo.goods_pics=this.data.goodDetail.pics;
    this.GoodsInfo.goods_id=this.data.goodDetail.goods_id;

    console.log(this.GoodsInfo);*/

   


  },

  //图片预览
  handlePrevewImage(e){
    const urls =this.data.goodDetail.pics.map(v=>v.pics_mid);   
    const urlCurrent=e.currentTarget.dataset.url;
    //预览大图应该与预览大图数组的值一致 mid->mids small->small
    wx.previewImage({
      current: urlCurrent, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },

  //加入购物车
  handleCartAdd(){
    let cart=wx.getStorageSync('cart')||[];
    //findIndex()查询到值返回0，查询值不存在返回-1
    let index=cart.findIndex(v=>v.goods_id===this.GoodsInfo.goods_id); 
    if(index===-1){ 
      this.GoodsInfo.num=1; 
      this.GoodsInfo.checked=true;
      cart.push(this.GoodsInfo);
    }
    else{
      cart[index].num++;
    }
    wx.setStorageSync('cart', cart);
    wx.showToast({
      title: '加入购物车成功',
      icon:'success',
      mask:true
    })
  },

  //调用接口获取商品详情页信息
async getGoodsDetail(goods_id){
    const res = await request({ url:"/goods/detail",data:{goods_id}});
    const goodDetail=res.data.message;

    //GoodsInfo存放goodsDetail原始数据，而goodsDetail为了性能需要简化
    this.GoodsInfo=goodDetail;
   
    let collect=wx.getStorageSync('collect')||[];
    //some()只要有一个为true返回true ，every()所有为true才返回true
    let isCollect=collect.some(v=>v.goods_id===this.GoodsInfo.goods_id);
   
    this.setData({
      goodDetail:{
        goods_name:goodDetail.goods_name,
        goods_price:goodDetail.goods_price,
        //苹果无法识别webp后缀的图片，转义其为jpg
        goods_introduce:goodDetail.goods_introduce.replace(/\.webp/g,'.jpg'),
        pics:goodDetail.pics,
        isCollect:isCollect
      }
    })
    wx.stopPullDownRefresh()
  },

//收藏与取消收藏
  async handleCollect(){
    let isCollect=false;   
    let collect=wx.getStorageSync('collect')||[];
    let index=collect.findIndex(v=>v.goods_id===this.GoodsInfo.goods_id);
    if(index!==-1){
      collect.splice(index,1);
      isCollect=false;
      await showToast({title:'取消收藏'});
    }
    else{
      collect.push(this.GoodsInfo);
      isCollect=true;
      await showToast({title:'收藏成功'})
    }
    wx.setStorageSync("collect",collect);
    this.setData({
      isCollect:isCollect,
      
      
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取页面栈及其数据
    let pages=getCurrentPages();
    let currentPage=pages[pages.length-1];
    const goods_id=currentPage.options.goods_id;
   
    this.getGoodsDetail(goods_id);
   

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})